package com.huihuiyun.huihuiyun.server.controller.member.client;

import com.huihuiyun.huihuiyun.server.controller.member.client.viewobjects.HhyAreaVO;
import com.huihuiyun.huihuiyun.server.domain.HhyArea;
import com.huihuiyun.huihuiyun.server.service.HhyAreaService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

@Component
@Path(Const.context)
@Slf4j
public class MemberClientAreaController {
    @Autowired
    private HhyAreaService areaService;

    /*******************
     * area
     ******************/
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/area/list")
    public ResultVO<List<HhyAreaVO>> areaList() {
        try {
            List<HhyArea> areaList = areaService.findAll();
            Map provinceManager = new HashMap<>();
            for (HhyArea area : areaList) {
                if (area.city.equals(HhyArea.NONE) && area.district.equals(HhyArea.NONE)) {
                    String value = area.id;
                    String province = area.province;
                    Map map = new HashMap();
                    map.put("value", value);
                    map.put("label", province);
                    map.put("children", new Vector<>());
                    provinceManager.put(province, map);
                }
            }

            Map cityManager = new HashMap();
            for (HhyArea area : areaList) {
                if (!area.city.equals(HhyArea.NONE) && area.district.equals(HhyArea.NONE)) {
                    String value = area.id;
                    String province = area.province;
                    String city = area.city;

                    Map map = new HashMap();
                    map.put("value", value);
                    map.put("label", city);
                    map.put("children", new Vector<>());
                    cityManager.put(city, map);

                    Map provinceMap = (Map) provinceManager.get(province);
                    List cityList = (List) provinceMap.get("children");
                    cityList.add(map);
                }
            }

            for (HhyArea area : areaList) {
                if (!area.city.equals(HhyArea.NONE) && !area.district.equals(HhyArea.NONE)) {
                    String value = area.id;
                    String city = area.city;
                    String district = area.district;

                    Map map = new HashMap();
                    map.put("value", value);
                    map.put("label", district);
                    map.put("children", new Vector<>());

                    Map cityMap = (Map) cityManager.get(city);
                    List districtList = (List) cityMap.get("children");
                    districtList.add(map);
                }
            }
            List list = new Vector();
            list.addAll(provinceManager.values());
            List<HhyAreaVO> hhyAreaVOList = ResultVOUtil.convertList(list, HhyAreaVO.class);
            return ResultVOUtil.success(hhyAreaVOList);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取区域信息失败");
        }
    }

}